<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="4">
            <a-tooltip placement="topLeft" overlayClassName="custom-tooltip">
              <template slot="title">
                <p style="margin-bottom: 0">查询技巧：</p>
                <p style="margin-bottom: 0">1.多选查询：多个查询字段之间用逗号","分隔。</p>
                <span>例如：</span>
                <span style="margin-bottom: 0;color: greenyellow">CP.000002，SC.000002</span>
              </template>
              <a-form-item label="物料代码">
                <a-input placeholder="请输入物料代码" v-model="queryParam.materialCode"></a-input>
              </a-form-item>
            </a-tooltip>
          </a-col>
          <a-col :md="6" :sm="4">
            <a-tooltip arrow-point-at-center>
              <template slot="title">
                <p style="margin-bottom: 0">查询技巧：</p>
                <p style="margin-bottom: 0">
                  1.多选查询：多个查询字段之间用逗号","分隔。例如：
                  <span style="color: greenyellow">散热器，喷涂件</span>
                </p>
                <p style="margin-bottom: 0">
                  2.模糊查询：一个查询字段之间用管道符号"|"分隔。例如：
                  <span style="color: greenyellow">散热器|喷涂件</span>
                </p>
              </template>
              <a-form-item label="物料名称">
                <a-input placeholder="请输入物料名称" v-model="queryParam.materialName"></a-input>
              </a-form-item>
            </a-tooltip>
          </a-col>
          <a-col :md="6" :sm="4">
            <a-form-item label="物料类型">
              <j-multi-select-tag v-model="queryParam.materialType" dictCode="hf_material_type,material_type_name,id" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="4">
            <a-form-item label="物料仓库">
              <j-multi-select-tag v-model="queryParam.warehouse" dictCode="hf_warehouse,warehouse_name,id" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button icon="search" type="primary" @click="searchQuery">查询</a-button>
      <a-button icon="reload" type="primary" @click="searchReset">重置</a-button>
      <a-button icon="plus" type="primary" @click="handleAdd">新增</a-button>
      <a-button icon="download" type="primary" @click="handleExportXls(`物料`)">导出当前全部数据</a-button>
      <a-upload
        v-has="'material:import'"
        :action="importExcelUrl"
        :headers="tokenHeader"
        :multiple="false"
        :showUploadList="false"
        name="file"
        @change="handleImportExcel"
      >
        <a-button icon="import" type="primary">导入</a-button>
      </a-upload>
      <a-button
        style="background: #FF8F00;border: #FF8F00;"
        icon="import"
        type="primary"
        @click="uploadModalJD.visible = true"
        >金蝶导入</a-button
      >
      <a-modal
        :visible="uploadModalJD.visible"
        @cancel="
          () => {
            uploadModalJD.visible = false
          }
        "
      >
        <a-spin :spinning="uploadModalJD.loading">
          <a-row :gutter="[16, 16]">
            <a-col :span="24">
              <a-upload :multiple="false" :showUploadList="false" name="file" :customRequest="customUpload">
                <a-button icon="import" type="primary">选择金蝶导出的文件</a-button>
              </a-upload>
            </a-col>
          </a-row>
        </a-spin>
      </a-modal>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider style="background-color: black" type="vertical" />
          <a @click="handleDelete(record.id)">删除</a>
          <a-divider style="background-color: black" type="vertical" />
          <a @click="handleCopyAdd(record)">复制</a>
        </span>
      </a-table>
    </div>

    <hf-material-modal ref="modalForm" @ok="modalFormOk"></hf-material-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfMaterialModal from './modules/HfMaterialModal'
import dayjs from 'dayjs'
import { getAction, postAction } from '@api/manage'

export default {
  name: 'HfMaterialList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    HfMaterialModal
  },
  data() {
    return {
      description: '物料管理页面',
      uploadModalJD: {
        visible: false,
        loading: false
      },
      isorter: {
        column: 'createTime',
        order: 'desc'
      },
      // 表头
      columns: [
        {
          title: '物料代码',
          align: 'center',
          dataIndex: 'materialCode'
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'sap'
        },
        {
          title: '物料类型',
          align: 'center',
          dataIndex: 'materialType_dictText'
        },

        {
          title: '单箱数',
          align: 'center',
          dataIndex: 'perNumber'
        },
        {
          title: '默认仓库',
          align: 'center',
          dataIndex: 'warehouse_dictText'
        },
        {
          title: '物料计量单位',
          align: 'center',
          dataIndex: 'unit_dictText'
        },
        {
          title: '最大库存',
          align: 'center',
          dataIndex: 'maxStorage'
        },
        {
          title: '安全库存',
          align: 'center',
          dataIndex: 'safeStorage'
        },
        {
          title: '最小库存',
          align: 'center',
          dataIndex: 'minStorage'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/material/hfMaterial/list',
        delete: '/material/hfMaterial/delete',
        deleteBatch: '/material/hfMaterial/deleteBatch',
        exportXlsUrl: '/material/hfMaterial/exportXls',
        importExcelUrl: 'material/hfMaterial/importExcel',
        importExcelUrlByJD: '/material/hfMaterial/importExcelByJD'
      },
      dictOptions: {},
      superFieldList: []
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    importExcelUrlByJD: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrlByJD}`
    }
  },
  methods: {
    dayjs,
    initDictConfig() {},
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      this.loading = true
      getAction(this.url.list, params)
        .then(res => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleCopyAdd(record) {
      this.$refs.modalForm.copyAdd(record)
      this.$refs.modalForm.title = '复制新增'
      this.$refs.modalForm.disableSubmit = false
    },
    // websocketOnmessage: function (e) {
    //   console.log("-----接收消息-------", e.data);
    //   var data = eval("(" + e.data + ")"); //解析对象
    //   if (data.cmd === "topic") {
    //     //系统通知
    //   } else if (data.cmd === "user") {
    //     //用户消息
    //     let msg = JSON.parse(e.data)
    //     this.$message.success(msg.msgTxt)
    //   }
    //   //心跳检测重置
    //   //this.heartCheck.reset().start();
    // },
    customUpload(options) {
      this.uploadModalJD.loading = true
      const { file, onProgress, onSuccess, onError } = options
      const formData = new FormData()
      formData.append('file', file)
      postAction(this.importExcelUrlByJD, formData, { headers: this.tokenHeader, timeout: 0 })
        .then(res => {
          if (res.success) {
            this.$message.success(res.message)
            this.close()
          } else {
            this.$message.warning(res.message)
          }
        })
        .catch(err => {
          console.log('err', err)
        })
        .finally(() => {
          this.uploadModalJD.visible = false
          this.uploadModalJD.loading = false
        })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';

.custom-tooltip {
  width: 200px; /* 设置你需要的宽度 */
  background-color: yellow; /* 可选：设置背景颜色以示区别 */
}
</style>
